import uniCrazyRouter, {
	beforeEach,
	afterEach,
	onError,
	afterNotNext
} from "uni-crazy-router";
import {
	bindInterceptLogin,
	destroyInterceptLogin
} from './interceptLogin';

export function setupRouter(app) {
	// console.log('setupRouter.v3', app);
	
	// 接收vue3的实例，并注册uni-crazy-router
	app.use(uniCrazyRouter);

	// 启用登录拦截
	bindInterceptLogin();

	beforeEach(async (to, from, next) => {
		// 逻辑代码
		// console.log('beforeEach.v3', {to, from, next});

		next()
	});

	afterEach((to, from) => {
		// 逻辑代码
		// console.log('afterEach.v3', {to, from});
		
	});

	// 除了在beforeEach钩子里没有使用next导致的无法跳转的情况外，其他情况导致的跳转失败都会触发onError
	onError((to, from) => {
		// console.log('onError.v3', {to, from});
		uni.showModal({
			title: '路由错误',
			content: `${to.url} 不存在`,
			showCancel: false
		});
	});
}
